# !/usr/bin/python3
# -*- encoding: utf-8 -*-
"""
@File        : forms.py
@Time        : 2022/5/6 23:46
@Author      : yang xin
@Software    : PyCharm
@Description : 
"""
from django import forms
from django.core.exceptions import ValidationError

from blogs.models import User


class LoginForm(forms.Form):
    username = forms.CharField(max_length=32, label="用户名")
    password = forms.CharField(max_length=32, label="密码")


class CreateUserForm(forms.Form):
    username = forms.CharField(max_length=32, label="用户名")
    password = forms.CharField(max_length=32, label="密码")
    phone = forms.CharField(min_length=11, max_length=11, label="电话")
    detail = forms.JSONField(label="用户详情")

    def clean_username(self):
        if User.objects.filter(username=self.cleaned_data["username"]).exists():
            raise ValidationError("该用户名已存在,请重新输入")
        return self.cleaned_data["username"]


class CreateArticleForm(forms.Form):
    username = forms.CharField(max_length=32, label="用户名")
    title = forms.CharField(max_length=100, label="标题")
    content = forms.CharField(label="内容")